package com.ikkong.huoyuan.controller;

import java.util.Date;
import java.util.Map;
import com.ikkong.core.base.BaseController;
import com.ikkong.core.jfinal.ext.kit.JsonKit;
import com.ikkong.core.jfinal.ext.shiro.ClearShiro;
import com.ikkong.core.jfinal.ext.shiro.ShiroKit;
import com.ikkong.system.controller.base.UrlPermissController;
import com.ikkong.huoyuan.model.Huoyuan;
import com.ikkong.huoyuan.service.HuoyuanService;
import com.ikkong.huoyuan.service.impl.HuoyuanServiceImpl;
import com.ikkong.xuqiu.model.Xuqiu;
import org.apache.shiro.authz.annotation.RequiresUser;

/**
 * Generated by Blade.
 * 2020-02-14 22:21:25
 */
public class HuoyuanController extends UrlPermissController {
	private static String CODE = "huoyuan";
	private static String PERFIX = "huoyuan";
	private static String LIST_SOURCE = "Huoyuan.list";
	private static String BASE_PATH = "/platform/huoyuan/";
	
	HuoyuanService service = new HuoyuanServiceImpl();
	
	public void index() {
		setAttr("code", CODE);
		render(BASE_PATH + "huoyuan.html");
	}

	public void add() {
		setAttr("code", CODE);
		render(BASE_PATH + "huoyuan_add.html");
	}

	public void edit() {
		String id = getPara(0);
		Huoyuan huoyuan = service.findById(id);
		setAttr("model", JsonKit.toJson(huoyuan));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "huoyuan_edit.html");
	}

	public void view() {
		String id = getPara(0);
		Huoyuan huoyuan = service.findById(id);
		setAttr("model", JsonKit.toJson(huoyuan));
		setAttr("id", id);
		setAttr("code", CODE);
		render(BASE_PATH + "huoyuan_view.html");
	}

	public void list() {
		Object grid = paginate(LIST_SOURCE);
		renderJson(grid);
	}

	@ClearShiro
	@RequiresUser
	public void shenhe(){
		Integer id = getParaToInt("id");
		Huoyuan huoyuan = new Huoyuan();
		huoyuan.setId(id);
		huoyuan.setStatus(2);
		boolean update = service.update(huoyuan);
		if (update) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void save() {
		Huoyuan huoyuan = mapping(PERFIX, Huoyuan.class);
		huoyuan.setAddtime(new Date());
		huoyuan.setU_id(Integer.parseInt(ShiroKit.getUser()!=null?ShiroKit.getUser().getId().toString():"2"));
		boolean temp = service.save(huoyuan);
		if (temp) {
			renderJson(success(SAVE_SUCCESS_MSG));
		} else {
			renderJson(error(SAVE_FAIL_MSG));
		}
	}

	public void update() {
		Huoyuan huoyuan = mapping(PERFIX, Huoyuan.class);
		boolean temp = service.update(huoyuan);
		if (temp) {
			renderJson(success(UPDATE_SUCCESS_MSG));
		} else {
			renderJson(error(UPDATE_FAIL_MSG));
		}
	}

	public void remove() {
		String ids = getPara("ids");
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			renderJson(success(DEL_SUCCESS_MSG));
		} else {
			renderJson(error(DEL_FAIL_MSG));
		}
	}
}
